package com.liu.community.server.impl;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 20:03 2021/10/16
 */

import com.liu.community.dto.NotificationDto;
import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.enums.NotificationStatusEnum;
import com.liu.community.enums.NotificationTypeEnum;
import com.liu.community.exception.CustomerException;
import com.liu.community.exception.ErrorCode;
import com.liu.community.mapper.NotificationMapper;
import com.liu.community.mapper.UserMapper;
import com.liu.community.pojor.Notification;
import com.liu.community.pojor.User;
import com.liu.community.server.NotificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageDto<NotificationDto> list(Integer userId, Integer currentPage, Integer pageSize) {
        PageDto<NotificationDto> pageDto = new PageDto<>();
        List<NotificationDto> notificationDtoList = new ArrayList<>();
        //求总记录数
        Integer dataTotalCount = notificationMapper.queryDataTotalCountByUserId(userId);
        //求begin边界
        Integer begin =  (currentPage - 1) * pageSize;
        List<Notification> notificationList = notificationMapper.queryAllNotificationByUserId(userId,begin, pageSize);
        if (notificationList.size() == 0){
            return  pageDto;
        }
        //遍历集合
        for (Notification notification : notificationList) {
            //将notification 对象转化为notificationDto
            NotificationDto notificationDto = new NotificationDto();
            BeanUtils.copyProperties(notification,notificationDto);
            notificationDto.setTypeName(NotificationTypeEnum.nameOfTtype(notification.getType()));

            notificationDtoList.add(notificationDto);
        }
        pageDto.setData(notificationDtoList);
        pageDto.setPageMessage(currentPage,pageSize,dataTotalCount,pageDto);
        return pageDto;
    }

    @Override
    public Integer unreadCount(Integer userId) {
        Integer count = notificationMapper.queryUnReadCountById(userId);
        return count;
    }

    @Override
    public NotificationDto read(Integer id, User user) {
       Notification notification =  notificationMapper.queryNotificationById(id);
       if(notification == null){
           throw  new CustomerException(ErrorCode.NOTIFICATION_NOT_FOUND);
       }
       if (!notification.getReceiver().equals(user.getId())){
            throw new CustomerException(ErrorCode.READ_NOTIFICATION_FAIL);
       }
       Integer status =  NotificationStatusEnum.READ.getStatus();
       notificationMapper.updateRead(notification.getId(),status);
        NotificationDto notificationDto = new NotificationDto();
        BeanUtils.copyProperties(notification,notificationDto);
        notificationDto.setTypeName(NotificationTypeEnum.nameOfTtype(notification.getType()));
        return notificationDto;
    }
}
